package org.dc.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.dc.R;
import org.dc.entity.Shift;
import org.dc.service.PersistenceManager;
import org.dc.service.ShiftDao;
import org.dc.utlity.UIUtility;

/**
 * @user: Vladimir Chernishov
 * @date: 2/28/12
 * @time: 5:35 PM
 */
public class ShiftsScr extends Activity {

    private ShiftDao dao;
    private ViewGroup layout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dao = PersistenceManager.getShiftDao(this);
        ViewGroup layout = (ViewGroup) getLayoutInflater().inflate(R.layout.shifts, null);
        this.layout = (ViewGroup) layout.getChildAt(0);
        TableLayout table = (TableLayout) this.layout.getChildAt(1);

        int width = getWindowManager().getDefaultDisplay().getWidth() - 40;
        UIUtility.updateTableWidth(table, width);

        for (Shift shift : dao.getShifts()) {
            addShiftToTable(table, shift, width/4);
        }

        setContentView(layout);

    }

    private void addShiftToTable(final TableLayout table, final Shift shift, int width) {

        final TableRow row = new TableRow(this);
        TextView name = new TextView(this);
        name.setPadding(3, 3, 3, 3);
        name.setBackgroundColor(Color.WHITE);
        name.setText(shift.getName());
        TableRow.LayoutParams nameLayout = new TableRow.LayoutParams(width, TableRow.LayoutParams.WRAP_CONTENT);
        nameLayout.setMargins(1, 1, 1, 1);
        name.setLayoutParams(nameLayout);
        row.addView(name);

        TextView color = new TextView(this);
        color.setText(String.valueOf(shift.getColor()));
        color.setPadding(3, 3, 3, 3);
        color.setBackgroundColor(Color.WHITE);
        TableRow.LayoutParams colorLayout = new TableRow.LayoutParams(width, TableRow.LayoutParams.WRAP_CONTENT);
        colorLayout.setMargins(1, 1, 1, 1);
        color.setLayoutParams(colorLayout);
        row.addView(color);

        TextView from = new TextView(this);
        from.setText(String.valueOf(shift.getFrom()));
        from.setPadding(3, 3, 3, 3);
        from.setBackgroundColor(Color.WHITE);
        TableRow.LayoutParams fromLayout = new TableRow.LayoutParams(width, TableRow.LayoutParams.WRAP_CONTENT);
        fromLayout.setMargins(1, 1, 1, 1);
        from.setLayoutParams(fromLayout);
        row.addView(from);

        TextView till = new TextView(this);
        till.setText(String.valueOf(shift.getTill()));
        till.setPadding(3, 3, 3, 3);
        till.setBackgroundColor(Color.WHITE);
        TableRow.LayoutParams tillLayout = new TableRow.LayoutParams(width, TableRow.LayoutParams.WRAP_CONTENT);
        tillLayout.setMargins(1, 1, 1, 1);
        till.setLayoutParams(tillLayout);
        row.addView(till);

        TextView delete = new TextView(this);
        delete.setText("X");
        delete.setPadding(3, 3, 3, 3);
        delete.setBackgroundColor(Color.WHITE);
        delete.setTextColor(Color.RED);
        TableRow.LayoutParams deleteLayout = new TableRow.LayoutParams(20,
                TableRow.LayoutParams.WRAP_CONTENT);
        deleteLayout.setMargins(1, 1, 1, 1);
        delete.setLayoutParams(deleteLayout);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dao.delete(shift);
                table.removeView(row);
            }
        });
        row.addView(delete);

        table.addView(row);
    }

    @Override
    protected void onDestroy() {
        dao.close();
        super.onDestroy();
    }

    public void addShift(View v) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        final LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.add_new_shift, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                LinearLayout editTextColumn = (LinearLayout) layout.getChildAt(1);
                EditText nameText = (EditText)editTextColumn.getChildAt(0);
                EditText fromText = (EditText)editTextColumn.getChildAt(1);
                EditText tillText = (EditText)editTextColumn.getChildAt(2);
                addShift(nameText.getText().toString(), fromText.getText().toString(), tillText.getText().toString());
            }
        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    private void addShift(String name, String from, String till) {
        TableLayout table = (TableLayout) layout.getChildAt(1);
        Shift newShift = new Shift();
        newShift.setName(name);
        newShift.setFrom(Integer.decode(from));
        newShift.setTill(Integer.decode(till));
        newShift.setColor(1);
        dao.save(newShift);
        addShiftToTable(table, newShift, (getWindowManager().getDefaultDisplay().getWidth() - 20)/4);
        layout.invalidate();

    }


}